<div ng-controller="edituserC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">修改员工</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <span class="h4">编辑员工信息</span>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <label>用户名 <em class="text-muted">(2-15字符)</em></label>
                                <input type="text" class="form-control" ng-model="datauser.Name" required  value="{{datauser.Name}}" minlength="2" maxlength="15">
                            </div>
                            <div class="form-group">
                                <label>部门</label>
                                <select required class="form-control" ng-options=" x.Name for x in arr track by x.Id" ng-model="datauser.Department"></select>
                            </div>
                            <div class="form-group">
                                <label>职务(非必填)</label>
                                <select class="form-control" ng-options=" x.Value as x.Name for x in OfficesData" ng-model="datauser.Offices"></select>
                            </div>
                            <div class="form-group">
                                <label style="margin-right: 10px;">是否离职</label>
                                <label class="i-switch bg-info">
                                    <input type="checkbox" ng-model="datauser.IsLeave">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label style="margin-right: 10px;">是否锁定</label>
                                <label class="i-switch bg-info">
                                    <input type="checkbox" ng-model="datauser.IsLock">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label>登录名 <em class="text-muted">以字母开头的6-15位字母或数字(不能和用户名相同)</em></label>
                                <input type="text" class="form-control" ng-model="datauser.LoginName" ng-pattern="/^[a-z]\w{5,14}$/" required value="{{datauser.LoginName}}">
                            </div>
                            <div class="form-group">
                                <label>电话</label>
                                <input type="text" class="form-control" ng-model="datauser.Phone" required ng-pattern="/^[1][3-8]\d{9}$/" value="{{datauser.Phone}}">
                            </div>
                            <div class="form-group">
                                <label>身份证(非必填)</label>
                                <input type="text" class="form-control" ng-model="datauser.IDCard" ng-pattern="/^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/" value="{{datauser.IDCard}}">
                            </div>
                            <div class="form-group">
                                <label>生日(非必填)</label>
                                <!--<input type="text" class="form-control" ng-model="datauser.Birthday" ng-pattern="/^(^\d{4}-\d{2}-\d{2})$/" placeholder="例如2000-01-01" value="{{datauser.Birthday}}">-->
                                <input type="date" ng-model="Birthday" class="form-control">
                            </div>
                        </div>
                        <footer class="panel-footer text-right bg-light lter" style="text-align:center">
                            <button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="submituser()">提交</button>
                            <span style="padding:0 5%"></span>
                            <button type="button" class="btn btn-default" ng-click="abolish()">取消</button>
                        </footer>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
        //编辑员工控制器
    .controller("edituserC", function ($scope, $state, $stateParams, $http) {
        $scope.id = JSON.parse($stateParams.userdata);
        $http.get('../api/Department')
            .success(function (res) {
                $scope.departments = res.Data;
                console.log($scope.departments);
                $scope.traverseTree($scope.departments);
//                $scope.traverseTree(res.Data , 0);

        });
        //获取员工数据
        $http({
            url: "../api/Employee/Detail?id=" + $scope.id,
            method: "GET"
        })
            .then(function (res) {
                $scope.datauser = res.data;
                $scope.Birthday = new Date($scope.datauser.Birthday);
                console.log($scope.datauser);
            })
            .then(function (error) {
                if (error) {
                    console.log("error");
                }
            });
        $scope.arr = [];
        $scope.traverseTree = function(node){

            for (var i = 0; i < node.length; i++) {
                var empty = '　└─';
                for (var j = 1; j < node[i].Item1; j++) {
                    empty = '　　'+ empty
                }
                if(i != 0){
                    node[i].Item2.Name = empty+node[i].Item2.Name;
                }else{
                    node[i].Item2.Name = '　'+node[i].Item2.Name;
                }
                $scope.arr.push(node[i].Item2);
            }
        };
//递归算法
//        $scope.arr = [];
//        $scope.traverseTree = function(node,aa){
//            if (!node) {
//                return;
//            }
//            if (node && node.length > 0) {
//                var i = 0;
//                aa = aa +1;
//                for (i = 0; i < node.length; i++) {
//                    var item = node[i];
//                    var empty = '';
//                    for (index = 1; index < aa; index++) {
//                        if(index == (aa-1)){
//                            empty  = empty + '　└─'
//                        }else{
//                            empty  = empty + '　'
//                        }
//
//                    }
//                    item.Name = empty+item.Name;
////                    $scope.obj = {Name:empty+item.Name , Id: item.Id};
//                    $scope.arr.push(item);
////                    console.log($scope.arr);
//                    this.traverseTree(node[i].Children,aa);
//                }
//            }
//        };
        //提交编辑结果
        $scope.submituser = function () {
            $scope.datauser.DepartmentId = $scope.datauser.Department.Id;
            $scope.datauser.Birthday = $scope.Birthday;
            $http.put('../api/Employee', $scope.datauser)
                .success(function (res) {
                    if (res.State <= 0) {
                        alert(res.Msg);
                    } else {
                        alert("提交成功");
                        $state.go("app.Employee_List");
                    }
                });
        };
        //返回上一页
        $scope.abolish = function () {
            $state.go('app.Employee_List');
        };
    })
  
</script>